using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using log4net;
using Triton.Bot.Settings;
using Triton.Common.LogUtilities;

namespace Triton.Bot
{
	public class TaskManager
	{
		[CompilerGenerated]
		private sealed class Class207
		{
			public ITask itask_0;

			internal bool method_0(ITask itask_1)
			{
				return itask_1.Name == itask_0.Name;
			}
		}

		[CompilerGenerated]
		private sealed class Class208
		{
			public ITask itask_0;

			internal bool method_0(ITask itask_1)
			{
				return itask_1.Name == itask_0.Name;
			}
		}

		[CompilerGenerated]
		private sealed class Class209
		{
			public ITask itask_0;

			internal bool method_0(ITask itask_1)
			{
				return itask_1.Name == itask_0.Name;
			}
		}

		[CompilerGenerated]
		private sealed class Class210
		{
			public ITask itask_0;

			internal bool method_0(ITask itask_1)
			{
				return itask_1.Name == itask_0.Name;
			}
		}

		[CompilerGenerated]
		private sealed class Class211
		{
			public string string_0;

			internal bool method_0(ITask itask_0)
			{
				return itask_0.Name == string_0;
			}
		}

		[CompilerGenerated]
		private sealed class Class212
		{
			public string string_0;

			internal bool method_0(ITask itask_0)
			{
				return itask_0.Name == string_0;
			}
		}

		private static readonly ILog ilog_0 = Logger.GetLoggerInstanceForType();

		private bool bool_0;

		private readonly List<ITask> list_0 = new List<ITask>();

		public bool IsFrozen => bool_0;

		public List<ITask> TaskList => list_0.ToList();

		public void Freeze()
		{
			bool_0 = true;
		}

		public bool AddBefore(ITask task, string name)
		{
			if (task == null)
			{
				throw new ArgumentNullException("task");
			}
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[AddBefore] This TaskManager is frozen.");
				return false;
			}
			if (list_0.Any((ITask itask_1) => itask_1.Name == task.Name))
			{
				ilog_0.ErrorFormat("[AddBefore] A task with the name of {0} has already been added. This one will be skipped.", task.Name);
				return false;
			}
			int num = 0;
			while (true)
			{
				if (num < list_0.Count)
				{
					if (list_0[num].Name == name)
					{
						break;
					}
					num++;
					continue;
				}
				return false;
			}
			list_0.Insert(num, task);
			return true;
		}

		public bool AddAfter(ITask task, string name)
		{
			if (task == null)
			{
				throw new ArgumentNullException("task");
			}
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[AddAfter] This TaskManager is frozen.");
				return false;
			}
			if (list_0.Any((ITask itask_1) => itask_1.Name == task.Name))
			{
				ilog_0.ErrorFormat("[AddAfter] A task with the name of {0} has already been added. This one will be skipped.", task.Name);
				return false;
			}
			int num = 0;
			while (true)
			{
				if (num < list_0.Count)
				{
					if (list_0[num].Name == name)
					{
						break;
					}
					num++;
					continue;
				}
				return false;
			}
			list_0.Insert(num + 1, task);
			return true;
		}

		public bool SwapTasks(string name1, string name2)
		{
			if (name1 == null)
			{
				throw new ArgumentNullException("name1");
			}
			if (name2 == null)
			{
				throw new ArgumentNullException("name2");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[SwapTasks] This TaskManager is frozen.");
				return false;
			}
			int num = -1;
			int num2 = -1;
			for (int i = 0; i < list_0.Count; i++)
			{
				if (num != -1)
				{
					break;
				}
				if (num2 != -1)
				{
					break;
				}
				if (list_0[i].Name == name1)
				{
					num = i;
				}
				else if (list_0[i].Name == name2)
				{
					num2 = i;
				}
			}
			if (num != -1 && num2 != -1 && num != num2)
			{
				ITask value = list_0[num];
				list_0[num] = list_0[num2];
				list_0[num2] = value;
				return true;
			}
			ilog_0.ErrorFormat("[SwapTasks] A task could not be found, or both tasks are the same.");
			return false;
		}

		public bool Add(ITask task)
		{
			if (task == null)
			{
				throw new ArgumentNullException("task");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[Add] This TaskManager is frozen.");
				return false;
			}
			if (list_0.Any((ITask itask_1) => itask_1.Name == task.Name))
			{
				ilog_0.ErrorFormat("[Add] A task with the name of {0} has already been added. This one will be skipped.", task.Name);
				return false;
			}
			ilog_0.DebugFormat("[Add] Now adding [{0}] {1}.", task.Name, task.Description);
			list_0.Add(task);
			return true;
		}

		public bool AddAtFront(ITask task)
		{
			if (task == null)
			{
				throw new ArgumentNullException("task");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[AddAtFront] This TaskManager is frozen.");
				return false;
			}
			if (list_0.Any((ITask itask_1) => itask_1.Name == task.Name))
			{
				ilog_0.ErrorFormat("[AddAtFront] A task with the name of {0} has already been added. This one will be skipped.", task.Name);
				return false;
			}
			ilog_0.DebugFormat("[AddAtFront] Now adding [{0}] {1}.", task.Name, task.Description);
			list_0.Insert(0, task);
			return true;
		}

		public bool Remove(string name)
		{
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[Remove] This TaskManager is frozen.");
				return false;
			}
			ITask task = list_0.FirstOrDefault((ITask itask_0) => itask_0.Name == name);
			if (task == null)
			{
				ilog_0.ErrorFormat("[Remove] A task with the name of {0} does not exist.", name);
				return false;
			}
			ilog_0.DebugFormat("[Remove] Now removing [{0}].", task.Name);
			list_0.Remove(task);
			return true;
		}

		public ITask GetTaskByName(string name)
		{
			return list_0.FirstOrDefault((ITask itask_0) => itask_0.Name == name);
		}

		public bool Replace(string name, ITask task)
		{
			if (task == null)
			{
				throw new ArgumentNullException("task");
			}
			if (name == null)
			{
				throw new ArgumentNullException("name");
			}
			if (IsFrozen)
			{
				ilog_0.ErrorFormat("[Replace] This TaskManager is frozen.");
				return false;
			}
			int num = 0;
			while (true)
			{
				if (num < list_0.Count)
				{
					if (list_0[num].Name == name)
					{
						break;
					}
					num++;
					continue;
				}
				ilog_0.ErrorFormat("[Replace] A task with the name of {0} was not found.", name);
				return false;
			}
			int num2 = 0;
			while (true)
			{
				if (num2 < list_0.Count)
				{
					if (num2 != num && list_0[num2].Name == task.Name)
					{
						break;
					}
					num2++;
					continue;
				}
				ilog_0.DebugFormat("[Replace] Now replacing task [{0}] with [{1}].", name, task.Name);
				list_0[num] = task;
				return true;
			}
			ilog_0.ErrorFormat("[Replace] A task with the name of {0} has already been registered.", task.Name);
			return false;
		}

		public void Start()
		{
			foreach (ITask item in list_0.ToList())
			{
				item.Start();
			}
		}

		public void Tick()
		{
			foreach (ITask item in list_0.ToList())
			{
				item.Tick();
			}
		}

		public void Stop()
		{
			foreach (ITask item in list_0.ToList())
			{
				item.Stop();
			}
		}

		public void Reset()
		{
			list_0.Clear();
			bool_0 = false;
		}

		public async Task<bool> Execute(string type, params dynamic[] param)
		{
			bool flag = type.EndsWith("_event");
			bool result = false;
			List<ITask> list = list_0.ToList();
			foreach (ITask item in list)
			{
				if (!(await item.Logic(type, param)))
				{
					continue;
				}
				if (flag)
				{
					result = true;
					continue;
				}
				if (GlobalSettings.Instance.DebugLastTask)
				{
					StringBuilder stringBuilder = new StringBuilder();
					foreach (ITask item2 in list)
					{
						if (item2 != item)
						{
							stringBuilder.AppendFormat("[{0}] -> ", item2.Name);
							continue;
						}
						break;
					}
					ilog_0.InfoFormat("[Execute] {1} {0}.", item.Name, stringBuilder.ToString());
				}
				return true;
			}
			return result;
		}
	}
}
